package com.sharer.last.support;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 通用字段自动填充
 *
 * @ClassName CommonFieldFillHandler
 * @Author WangJin
 * @Date 2023/04/28/10:20
 * @Description
 * @Version 1.0
 */
public class CommonFieldFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime curDateTime = LocalDateTime.now();
        Object        createTime  = this.getFieldValByName("createTime", metaObject);
        if (null == createTime) {
            this.setFieldValByName("createTime", curDateTime, metaObject);
        }
        Object updateTime = this.getFieldValByName("updateTime", metaObject);
        if (null == updateTime) {
            this.setFieldValByName("updateTime", curDateTime, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime curDateTime = LocalDateTime.now();
        Object        updateTime  = this.getFieldValByName("updateTime", metaObject);
        if (null == updateTime) {
            this.setFieldValByName("updateTime", curDateTime, metaObject);
        }
    }
}

